package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.crm.mapper.GwcqTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import static java.math.BigDecimal.ROUND_HALF_DOWN;
import static java.math.BigDecimal.ROUND_HALF_UP;

/**
 * 购物车 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-17
 */
@Service
public class Gwai700ServiceImpl extends ServiceImpl<GwcqTMapper, GwcqT> {
    private String YUEHUILV = "1";
    private String RIHUILV = "2";

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
//        List<Dto> listData = this.baseMapper.getListData(page, dto);
        List<Dto> listData = InvokeUtil.invokeBeanMethod("gwcqTMapper,getListData",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto getAddData(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //客户权限
        //dto.put("CUSTOMERS", getCustomersAuthority(dto));
        //部门权限
        //dto.put("ORGS", getOrgsAuthority(dto));
        List<Dto> dtoList = this.baseMapper.getAddData(dto);
        if (dtoList.size() > 0) {
            Dto dataDto = dtoList.get(0);
            dataDto.put("GWCQ010", dataDto.getString("GWAK001"));
            dataDto.put("GWCQ010_NAME", dataDto.getString("GWAKL004"));
            dataDto.put("GWCQ006", dataDto.getString("GWAK008"));
        } else {
            Dto dataDto = Dtos.newDto();
            dtoList.add(dataDto);
        }
        Date date = new Date();
        dtoList.get(0).put("GWCQDOCNO", "GWDD" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss));
        dtoList.get(0).put("GWCQDOCDATE", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        dtoList.get(0).put("GWCQ014", shiroUser.getID());
        dtoList.get(0).put("GWCQ014_NAME", shiroUser.getNAME());
        dtoList.get(0).put("GWCQDOCDATE", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        return dtoList.get(0);
    }

    @Transactional(readOnly = true)
    public List<Dto> getUpdateData(Dto dto) {
        if (1 == 2) {
            //客户权限
            //dto.put("CUSTOMERS", getCustomersAuthority(dto));
            //部门权限
            //dto.put("ORGS", getOrgsAuthority(dto));
        }
        List<Dto> listData = this.baseMapper.getUpdateData(dto);
        return listData;
    }

    @Transactional
    public void addData(List<GwcqT> gwcqTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto customerData = this.getCustomerData(gwcqTList.get(0).getGWCQ010());
        //增加新的明细
        for (int i = 0; i < gwcqTList.size(); i++) {
            initData(gwcqTList.get(i), i + 1, shiroUser);
            gwcqTList.get(i).setGWCQ006(customerData.getString("biZhong"));
            gwcqTList.get(i).insert();
        }
    }


    @Transactional
    public void saveData(GwcqT gwcqTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto customerData = this.baseMapper.getBizhong(gwcqTList.getGWCQ010());
        //增加新的明细
        Date date = new Date();
        String gwcqdocno = "GWDD" + DateUtils.formatDate(date, "yyyyMMddHHmmss");
        LocalDate gwcqdocdate = LocalDate.now();
        gwcqTList.setGWCQDOCNO(gwcqdocno);
        gwcqTList.setGWCQDOCDATE(gwcqdocdate);
        initData(gwcqTList, 1, shiroUser);
        gwcqTList.setGWCQ006(customerData.getString("GWAK008"));
        gwcqTList.setGWCQPLANT(customerData.getString("GWAKPLANT"));
        gwcqTList.setGWCQLEGAL(customerData.getString("GWAKPLANT"));
        gwcqTList.setGWCQ103(customerData.getBigDecimal("GWAK056"));
        gwcqTList.setGWCQ104(gwcqTList.getGWCQ004());
        gwcqTList.setGWCQACTI("Y");
        gwcqTList.setGWCQ101(DateUtils.getDate());
        gwcqTList.setGWCQ114("1");
        gwcqTList.setGWCQ115("N");
        gwcqTList.insert();

    }

    @Transactional
    public void editData(List<GwcqT> gwcqTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto customerData = this.getCustomerData(gwcqTList.get(0).getGWCQ010());
        //删除旧的明细
        Dto deleteDto = Dtos.newDto();
        deleteDto.put("ENT", shiroUser.getDBC());
        deleteDto.put("GWCQDOCNO", gwcqTList.get(0).getGWCQDOCNO());
        this.baseMapper.deleteDataByDocno(deleteDto);
        //增加新的明细
        for (int i = 0; i < gwcqTList.size(); i++) {
            initData(gwcqTList.get(i), i + 1, shiroUser);
            gwcqTList.get(i).setGWCQ006(customerData.getString("biZhong"));
            gwcqTList.get(i).insert();
        }
    }

    private void initData(GwcqT gwcqT, Integer index, ShiroUser shiroUser) {
        gwcqT.setGWCQENT(shiroUser.getDBC());
        gwcqT.setGWCQ008(shiroUser.getNAME());
        gwcqT.setGWCQ014(shiroUser.getID());
        gwcqT.setGWCQSEQ(BigDecimal.valueOf(index));
        gwcqT.setGWCQUSER(shiroUser.getID());
        gwcqT.setGWCQGRUP(shiroUser.getORG_ID());
        gwcqT.setGWCQDATE(LocalDate.now());
        gwcqT.setGWCQTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwcqT.setGWCQORIG(shiroUser.getORG_ID());
        gwcqT.setGWCQORIU(shiroUser.getID());
    }

    @Transactional
    public void deleteData(GwcqT gwcqT) {
        delete(gwcqT);
    }

    @Transactional
    public void deleteDataBatch(List<GwcqT> gwcqTList) {
        for (GwcqT gwcqT : gwcqTList) {
            delete(gwcqT);
        }
    }

    private void delete(GwcqT gwcqT) {
        UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
        gwcqTUpdateWrapper.eq("GWCQENT", gwcqT.getGWCQENT());
        gwcqTUpdateWrapper.eq("GWCQDOCNO", gwcqT.getGWCQDOCNO());
        /*gwcqTUpdateWrapper.eq("GWCQSEQ",gwcqT.getGWCQSEQ());*/
        gwcqT.delete(gwcqTUpdateWrapper);
    }

    public void deleteGwcq(GwcqT gwcqT) {
        UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
        gwcqTUpdateWrapper.eq("GWCQDOCNO", gwcqT.getGWCQDOCNO());
        gwcqTUpdateWrapper.eq("GWCQSEQ", gwcqT.getGWCQSEQ());
        gwcqT.delete(gwcqTUpdateWrapper);
    }

    /**
     * 转订单
     *
     * @param gwcqTList
     */
    @Transactional
    public void transformOrder(List<GwcqT> gwcqTList) {
        ShiroUser user = ShiroKit.getUser();
        Date date = new Date();
        //删除重复单号.然后根据单列表查单身进行转订单
        List<GwcqT> docnoList = new ArrayList<>();
        for (GwcqT gwcqT : gwcqTList) {
            Boolean has = false;
            for (GwcqT docno : docnoList) {
                if (docno.getGWCQDOCNO().equals(gwcqT.getGWCQDOCNO())) {
                    has = true;
                }
            }
            if (!has) {
                docnoList.add(gwcqT);
            }
        }
        List<String> sList = new ArrayList<>();
        for (int i = 0; i < docnoList.size(); i++) {
            if (docnoList.get(i).getGWCQ010() != null) {
                sList.add(docnoList.get(i).getGWCQ010() + "/" + docnoList.get(i).getGWCQ109());
            }
        }
        List<String> listNew = new ArrayList<>(new HashSet(sList));
        for (String data : listNew) {
            String[] list = data.split("/");
            List<Dto> list1 = new ArrayList<>();
            Dto inDto = Dtos.newDto();
            inDto.put("GWCQ010", list[0]);
            List<String> gwcq = new ArrayList<>();;
            for (GwcqT gwcqT : docnoList) {
                String gwcq109 = "";
                if (!(gwcqT.getGWCQ109() == null)) {
                    gwcq109 = gwcqT.getGWCQ109();
                }else{
                    gwcq109 = "null";
                }
                if (gwcqT.getGWCQ010().equals(list[0]) && gwcq109.equals(list[1])) {
                    gwcq.add(gwcqT.getGWCQDOCNO());
                }
            }
            if(gwcq.size() == 0){
                gwcq = null;
            }
            inDto.put("gwcq", gwcq);
            if (list[1].equals("null")) {
//                list1 = this.baseMapper.getGwcqList2(inDto);
                list1 = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList2",inDto);
            } else {
                inDto.put("GWCQ109", list[1]);
//                list1 = this.baseMapper.getGwcqList1(inDto);
                list1 = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList1",inDto);
            }
            Date now = new Date();
            String GWCSDOCNO = "ZS" + DateUtils.formatDate(now, DateUtils.PATTERN_yyyyMMddHHmmssSSS);
            for (int i = 0; i < list1.size(); i++) {
                GwcsT gwcs = new GwcsT();
                gwcs.setGWCSENT(user.getDBC());
                gwcs.setGWCSDOCNO(GWCSDOCNO);
                gwcs.setGWCSSEQ(new BigDecimal(i + 1));
                gwcs.setGWCSDOCDATE(LocalDate.now());
                gwcs.setGWCS001(list1.get(i).getString("GWCQ001"));
                gwcs.setGWCS002(list1.get(i).getString("GWCQ002"));
                gwcs.setGWCS003(list1.get(i).getBigDecimal("GWCQ003"));
                gwcs.setGWCS004(list1.get(i).getBigDecimal("GWCQ004"));
                gwcs.setGWCS005(list1.get(i).getBigDecimal("GWCQ005"));
                gwcs.setGWCS006(list1.get(i).getString("GWCQ006"));
                gwcs.setGWCS010(list[0]);
                gwcs.setGWCS014(user.getID());
                gwcs.setGWCS030(list1.get(i).getString("GWCQDOCNO"));
                gwcs.setGWCS031(list1.get(i).getBigDecimal("GWCQSEQ"));
                gwcs.setGWCSPLANT(list1.get(i).getString("GWCQPLANT"));
                gwcs.setGWCSLEGAL(list1.get(i).getString("GWCQLEGAL"));
                gwcs.setGWCSACTI("N");
                Dto dto = Dtos.newDto();
                dto.put("GWAK001", list[0]);
                Dto customerData = this.getCustomerData(list[0]);
                gwcs.setGWCS033(customerData.getString("huiLvLeiXing"));//汇率类型
                gwcs.setGWCS034(customerData.getBigDecimal("huiLv"));//汇率
//                if(list.get(i).getString("GWCQ113").equals("")){购物车会带回     g
//                    gwcs.setGWCS115(new BigDecimal(0));
//                    gwcs.setGWCS035(new BigDecimal(1));
//                }else{
                gwcs.setGWCS115(list1.get(i).getBigDecimal("GWCQ113"));
//                    gwcs.setGWCS035(list.get(i).getBigDecimal("GWCQ004").divide(gwcs.getGWCS115(),2,ROUND_HALF_DOWN));
                gwcs.setGWCS035(list1.get(i).getBigDecimal("GWCQ103"));
//                }
                Dto gwaf = this.baseMapper.getBizhong(list[0]);
                BigDecimal GWEA006 = new BigDecimal(0);
                if (!gwaf.getString("GWEA006").equals("")) {
                    GWEA006 = gwaf.getBigDecimal("GWEA006");
                }
                gwcs.setGWCS032(gwaf.getString("GWAK012"));
                gwcs.setGWCS036(gwcs.getGWCS005().multiply(gwcs.getGWCS035()));
                gwcs.setGWCS037(gwcs.getGWCS115().multiply(gwcs.getGWCS034()));
                gwcs.setGWCS038(gwcs.getGWCS005());
                gwcs.setGWCS039(gwcs.getGWCS038().multiply(gwcs.getGWCS035()));
                BigDecimal gwea006 = new BigDecimal(1).add(GWEA006.divide(new BigDecimal(100)));
                BigDecimal count = gwcs.getGWCS005().divide(gwea006, 10, ROUND_HALF_UP);
                gwcs.setGWCS040(gwcs.getGWCS005().subtract(count));
                gwcs.setGWCS041(gwcs.getGWCS040().multiply(gwcs.getGWCS034()));
                if(!list[1].equals("null")){
                    gwcs.setGWCS109(list[1]);
                }
                gwcs.setGWCS110(list1.get(i).getString("GWDC005"));
                gwcs.setGWCS111(list1.get(i).getString("GWDC004"));
                gwcs.setGWCS112(gwaf.getString("GWAS003"));
                gwcs.setGWCS113(gwaf.getString("GWAK058"));
                gwcs.setGWCS117(list1.get(i).getString("GWCQ114"));
                gwcs.setGWCS118(list1.get(i).getString("GWCQ115"));
                gwcs.setGWCS119("1");
                gwcs.setGWCSUSER(user.getID());
                gwcs.setGWCSGRUP(user.getORG_ID());
                gwcs.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                gwcs.setGWCSDATE(LocalDate.now());
                gwcs.setGWCSORIG(user.getORG_ID());
                gwcs.setGWCSORIU(user.getID());
                gwcs.insert();
                GwcqT gwcqT = new GwcqT();
                gwcqT.setGWCQDOCNO(list1.get(i).getString("GWCQDOCNO"));
                gwcqT.setGWCQSEQ(list1.get(i).getBigDecimal("GWCQSEQ"));
                gwcqT.setGWCQACTI("Z");
                UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
                gwcqTUpdateWrapper.eq("GWCQDOCNO", list1.get(i).getString("GWCQDOCNO"));
                gwcqTUpdateWrapper.eq("GWCQSEQ", list1.get(i).getString("GWCQSEQ"));
                gwcqT.update(gwcqTUpdateWrapper);
            }
            if (list1.size() > 0) {
                Dto queryDto = Dtos.newDto();
                queryDto.put("ENT", user.getDBC());
                queryDto.put("GWCQDOCNO", list1.get(0).getString("GWCQDOCNO"));
                List<GwcqzT> gwcqz_list = this.baseMapper.getGwcqzByDoc(queryDto);
                for (int i = 0; i < gwcqz_list.size(); i++) {
                    GwcqzT gwcqzT = gwcqz_list.get(i);
                    GwcszT gwcszT = new GwcszT();
                    gwcszT.setGWCSZENT(gwcqzT.getGWCQZENT());
                    gwcszT.setGWCSZDOCNO(GWCSDOCNO);
                    gwcszT.setGWCSZ003(String.valueOf(i + 1));
                    gwcszT.setGWCSZ004(gwcqzT.getGWCQZ002());
                    gwcszT.setGWCSZ005(gwcqzT.getGWCQZ003());
                    gwcszT.setGWCSZ006(gwcqzT.getGWCQZ004());
                    gwcszT.setGWCSZ007(gwcqzT.getGWCQZ005());
                    gwcszT.setGWCSZ008(gwcqzT.getGWCQZ006());
                    gwcszT.insert();
                }
            }
        }
//        String GWCSDOCNO = "ZSDD" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss);
//        int count = 1;
//        int countz = 1;
//        for (GwcqT docno : docnoList) {
//            Dto queryDto = Dtos.newDto();
//            queryDto.put("ENT", docno.getGWCQENT());
//            queryDto.put("GWCQDOCNO", docno.getGWCQDOCNO());
//            List<GwcqT> gwcq_list = this.baseMapper.getDataByDocno(queryDto);
//            List<GwcqzT> gwcqz_list = this.baseMapper.getGwcqzByDoc(queryDto);
//            Dto customerData = this.getCustomerData(docno.getGWCQ010());
//            queryDto.put("GWCQDOCDATE", docno.getGWCQDOCDATE());
//            queryDto.put("GWCQ010", docno.getGWCQ010());
//            Dto gwddData = this.baseMapper.getGwdd003(queryDto);
//            if (gwddData != null) {
//                if (gwddData.getString("GWDD003").equals("2")) {
//                    gwddData.put("GWCS112", "2");
//                } else {
//                    gwddData.put("GWCS112", "1");
//                }
//            }
//            //先根据经销商获取默认仓库，获取仓库类型
//            Dto gwak058 = this.baseMapper.getGwak058(queryDto);
//            /*if(gwak058 == null){
//                queryDto.put("GWCQ001",docno.getGWCQ001());
//                gwak058 = this.baseMapper.getGwam019(queryDto);
//            }*/
//            //获取返利类型
//            List<GwcsT> gwcsTList = generateOrder(gwcq_list, GWCSDOCNO, customerData, date, gwddData, gwak058);
//            List<GwcszT> gwcszTList = generateOrderGwcsz(gwcqz_list, GWCSDOCNO);
//            for (GwcsT gwcsT : gwcsTList) {
//                gwcsT.setGWCSSEQ(new BigDecimal(count));
//                gwcsT.insert();
//                count++;
//            }
//            for (GwcszT gwcszT : gwcszTList) {
//                gwcszT.setGWCSZSEQ(String.valueOf(countz));
//                gwcszT.insert();
//                count++;
//            }
//            queryDto.clear();
//            queryDto.put("ENT", docno.getGWCQENT());
//            queryDto.put("GWCQDOCNO", docno.getGWCQDOCNO());
//            this.baseMapper.setTransformOrderYes(queryDto);
//        }
    }

    /**
     * 部分-转订单
     *
     * @param gwcqTList
     */
    @Transactional
    public void transformOrderPart(List<GwcqT> gwcqTList) {
        Dto queryDto = Dtos.newDto();
        Date date = new Date();
        String GWCSDOCNO = "ZS" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss);
        Dto customerData = this.getCustomerData(gwcqTList.get(0).getGWCQ010());
        queryDto.put("GWCQDOCDATE", gwcqTList.get(0).getGWCQDOCDATE());
        queryDto.put("GWCQ010", gwcqTList.get(0).getGWCQ010());
//        Dto gwddData = this.baseMapper.getGwdd003(queryDto);
        Dto gwddData = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwdd003",queryDto);
        if (gwddData != null) {
            if (gwddData.getString("GWDD003").equals("2")) {
                gwddData.put("GWCS112", "2");
            } else {
                gwddData.put("GWCS112", "1");
            }
        }
        //先根据经销商获取默认仓库，否则根据产品带出默认仓库
        Dto gwak058 = this.baseMapper.getGwak058(queryDto);
        List<GwcsT> gwcsTList = generateOrder(gwcqTList, GWCSDOCNO, customerData, date, gwddData, gwak058);
        for (int i = 0; i < gwcsTList.size(); i++) {
            gwcsTList.get(i).insert();
            queryDto.clear();
            queryDto.put("ENT", gwcqTList.get(i).getGWCQENT());
            queryDto.put("GWCQDOCNO", gwcqTList.get(i).getGWCQDOCNO());
            queryDto.put("GWCQDSEQ", gwcqTList.get(i).getGWCQSEQ());
            this.baseMapper.setTransformInquiryYes(queryDto);
        }
    }

    /**
     * 订单统一生单逻辑
     *
     * @param gwcqTList
     */
    private List<GwcsT> generateOrder(List<GwcqT> gwcqTList, String GWCSDOCNO, Dto customerData, Date date, Dto gwddData, Dto gwak058) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String time = DateUtils.formatDate(date, "HH:mm:ss");
        List<GwcsT> gwcsTList = new ArrayList<>();//返回值
        String GWCQ101 = "";
        for (int i = 0; i < gwcqTList.size(); i++) {
            GWCQ101 = gwcqTList.get(0).getGWCQ101();
            GwcqT gwcqT = gwcqTList.get(i);
            GwcsT gwcsT = new GwcsT();
            gwcsT.setGWCSENT(gwcqT.getGWCQENT());
            gwcsT.setGWCSDOCNO(GWCSDOCNO);
            //gwcsT.setGWCSSEQ(BigDecimal.valueOf(i + 1));
            gwcsT.setGWCSDOCDATE(LocalDate.now());
            gwcsT.setGWCS001(gwcqT.getGWCQ001());//料号
            gwcsT.setGWCS002(gwcqT.getGWCQ002());//单位
            gwcsT.setGWCS003(gwcqT.getGWCQ003());//数量
            gwcsT.setGWCS004(gwcqT.getGWCQ004());//单价
            gwcsT.setGWCS005(gwcqT.getGWCQ005());//金额
            gwcsT.setGWCS006(customerData.getString("bizhong"));//币种 取客户资料币种
            gwcsT.setGWCS010(gwcqT.getGWCQ010());//客户 //渠道商
            //gwcsT.setGWCS011(gwcqT.getGWCQ011());//经销商
            //gwcsT.setGWCS016(gwcqT.getGWCQ016());//预计交货日期
            gwcsT.setGWCS017("");//合同文件链接
            gwcsT.setGWCS018(new BigDecimal(0));//合同金额
            gwcsT.setGWCS019("");//产品序列号
            gwcsT.setGWCS032(customerData.getString("shuiZhong"));//税种
            gwcsT.setGWCS033(customerData.getString("huiLvLeiXing"));//汇率类型
            gwcsT.setGWCS034(customerData.getBigDecimal("huiLv"));//汇率
            gwcsT.setGWCS035(gwcqT.getGWCQ103());//折扣率
            /*gwcsT.setGWCS035(gwcqT.getGWCQ103() == null ? BigDecimal.valueOf(1):gwcqT.getGWCQ103());
            gwcsT.setGWCS036(gwcsT.getGWCS005().multiply(gwcsT.getGWCS035()));*/
            gwcsT.setGWCS036(gwcqT.getGWCQ104());
            gwcsT.setGWCS037(gwcsT.getGWCS004().multiply(customerData.getBigDecimal("huiLv")));//本币单价
            gwcsT.setGWCS038(gwcsT.getGWCS005().multiply(customerData.getBigDecimal("huiLv")));//本币折扣前金额
            gwcsT.setGWCS039(gwcsT.getGWCS036().multiply(customerData.getBigDecimal("huiLv")));//本币折扣后金额
            //订单金额/（1+税率）*税率
            BigDecimal GWCS040 = gwcsT.getGWCS005().multiply(customerData.getBigDecimal("shuiLv").divide(new BigDecimal("100")).divide(
                    new BigDecimal(1).add(customerData.getBigDecimal("shuiLv").divide(new BigDecimal("100"))),
                    2, ROUND_HALF_UP));
            gwcsT.setGWCS040(GWCS040);
            gwcsT.setGWCS041(GWCS040.multiply(customerData.getBigDecimal("huiLv")));
            gwcsT.setGWCS043("");
            gwcsT.setGWCS044("");
            gwcsT.setGWCS045("");
            gwcsT.setGWCS104("");
            gwcsT.setGWCS109(gwcqT.getGWCQ109());
            gwcsT.setGWCS110(gwcqT.getGWCQ110());
            gwcsT.setGWCS111(gwcqT.getGWCQ111());
            /*gwcsT.setGWCS014("");
            gwcsT.setGWCS042(gwcqT.getGWCQ106());
            gwcsT.setGWCS046(gwcqT.getGWCQ105());*/
            /*if(gwddData != null ) {
                gwcsT.setGWCS112(gwddData.getString("GWCS112"));
                if (gwddData.getString("GWCS112").equals("2")) {
                    gwcsT.setGWCS116(gwddData.getBigDecimal("GWDD013"));
                }
            }*/
            if (gwak058 != null) {
                gwcsT.setGWCS112(gwak058.getString("GWAS003"));
                gwcsT.setGWCS113(gwak058.getString("GWAK058"));
            }/*else {
                Dto queryDto = Dtos.newDto();
                queryDto.put("ENT",gwcqT.getGWCQENT());
                queryDto.put("GWCQ001",gwcqT.getGWCQ001());
                gwak058 = this.baseMapper.getGwam019(queryDto);
                if(gwak058 != null){
                    gwcsT.setGWCS113(gwak058.getString("GWAK058"));
                }
            }*/
            /*gwcsT.setGWCS043(gwcqT.getGWCQDOCNO());
            gwcsT.setGWCS101(String.valueOf(gwcqT.getGWCQSEQ()));*/
            gwcsT.setGWCS114(gwcqT.getGWCQ112());
            gwcsT.setGWCS115(gwcqT.getGWCQ113());
            gwcsT.setGWCS116(new BigDecimal(0));
            if (gwcqT.getGWCQ109() != null) {
                Dto query = Dtos.newDto();
                query.put("ENT", shiroUser.getDBC());
                query.put("GWDC001", gwcqT.getGWCQ109());
                Dto gwdc005_006 = this.baseMapper.getGwdc005_006(query);
                if (gwdc005_006 != null) {
                    if (("1").equals(gwdc005_006.getString("GWDC005"))) {//裸价
                        if (("2").equals(gwdc005_006.getString("GWDC006")) || ("1").equals(gwdc005_006.getString("GWDC006"))) {
                            gwcsT.setGWCS117("2");
                        } else if (("3").equals(gwdc005_006.getString("GWDC006"))) {
                            gwcsT.setGWCS117("4");
                        }
                    } else {
                        if (("2").equals(gwdc005_006.getString("GWDC006")) || ("1").equals(gwdc005_006.getString("GWDC006"))) {
                            gwcsT.setGWCS117("1");
                        } else if (("3").equals(gwdc005_006.getString("GWDC006"))) {
                            gwcsT.setGWCS117("3");
                        }
                    }
                }
            } else {
                gwcsT.setGWCS117("1");
            }
            /*gwcsT.setGWCS117(gwcqT.getGWCQ114());
            gwcsT.setGWCS044("1");*/
            gwcsT.setGWCS016(LocalDate.parse(GWCQ101));//预交货日期
            gwcsT.setGWCS118(gwcqT.getGWCQ115());//拆包否
            gwcsT.setGWCS119("1");//1.正常新增
            gwcsT.setGWCSPLANT(gwcqT.getGWCQPLANT());
            gwcsT.setGWCSLEGAL(gwcqT.getGWCQLEGAL());
            gwcsT.setGWCSMLEGAL(gwcqT.getGWCQMLEGAL());
            gwcsT.setGWCSACTI("N");
            gwcsT.setGWCSUSER(shiroUser.getID());
            gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
            gwcsT.setGWCSDATE(LocalDate.now());
            gwcsT.setGWCSTIME(time);
            gwcsT.setGWCSORIU(shiroUser.getID());
            gwcsT.setGWCSORIG(shiroUser.getORG_ID());
            gwcsTList.add(gwcsT);
        }
        return gwcsTList;
    }

    /**
     * 订单赠品统一生单逻辑
     *
     * @param gwcqzTList
     */
    private List<GwcszT> generateOrderGwcsz(List<GwcqzT> gwcqzTList, String GWCSDOCNO) {
        ShiroUser shiroUser = ShiroKit.getUser();
        List<GwcszT> gwcszTList = new ArrayList<>();//返回值
        for (int i = 0; i < gwcqzTList.size(); i++) {
            GwcqzT gwcqzT = gwcqzTList.get(i);
            GwcszT gwcszT = new GwcszT();
            gwcszT.setGWCSZENT(gwcqzT.getGWCQZENT());
            gwcszT.setGWCSZDOCNO(GWCSDOCNO);
            gwcszT.setGWCSZ003(String.valueOf(i + 1));
            gwcszT.setGWCSZ004(gwcqzT.getGWCQZ002());
            gwcszT.setGWCSZ005(gwcqzT.getGWCQZ003());
            gwcszT.setGWCSZ006(gwcqzT.getGWCQZ004());
            gwcszT.setGWCSZ007(gwcqzT.getGWCQZ005());
            gwcszT.setGWCSZ008(gwcqzT.getGWCQZ006());
            gwcszTList.add(gwcszT);
        }
        return gwcszTList;
    }


    /**
     * 转询价单
     *
     * @param gwcqTList
     */
    @Transactional
    public void transformInquiry(List<GwcqT> gwcqTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto queryDto = Dtos.newDto();
        Date date = new Date();
        String GWEEDOCNO = "XJD" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss);
        Dto customerData = this.getCustomerData(gwcqTList.get(0).getGWCQ010());
        for (int i = 0; i < gwcqTList.size(); i++) {
            GwcqT gwcqT = gwcqTList.get(i);
            GweeT gweeT = new GweeT();
            gweeT.setGWEEENT(gwcqT.getGWCQENT());
            gweeT.setGWEEDOCNO(GWEEDOCNO);
            gweeT.setGWEESEQ(new BigDecimal(i + 1));
            gweeT.setGWEEDOCDATE(LocalDate.now());
            gweeT.setGWEE001(gwcqT.getGWCQ010());//客户
            gweeT.setGWEE002(gwcqT.getGWCQ001());//产品编号
            gweeT.setGWEE003(gwcqT.getGWCQ002());//销售单位
            gweeT.setGWEE004(gwcqT.getGWCQ003());//数量
            gweeT.setGWEE005(customerData.getString("shuiZhong"));//税种
            gweeT.setGWEE006(customerData.getString("huiLvLeiXing"));//汇率类型
            gweeT.setGWEE007(customerData.getBigDecimal("huiLv"));//汇率
            gweeT.setGWEE008(customerData.getBigDecimal("zheKou"));//折扣率
            gweeT.setGWEE009(gwcqT.getGWCQ004());//原币单价
            gweeT.setGWEE010(gwcqT.getGWCQ004().multiply(gwcqT.getGWCQ003()));//原币含税金额
            gweeT.setGWEE011(gweeT.getGWEE010().multiply(customerData.getBigDecimal("zheKou")));//原币含税折扣价
            gweeT.setGWEE012(gwcqT.getGWCQ004().divide(customerData.getBigDecimal("huiLv"), 3, ROUND_HALF_UP));//本币单价
            gweeT.setGWEE013(gweeT.getGWEE012().multiply(gwcqT.getGWCQ003()));//本币含税金额
            gweeT.setGWEE014(gweeT.getGWEE013().multiply(customerData.getBigDecimal("zheKou")));//本币含税折后价
            gweeT.setGWEE015(gweeT.getGWEE011().multiply(customerData.getBigDecimal("shuiLv")));//原币税额
            gweeT.setGWEE016(gweeT.getGWEE014().multiply(customerData.getBigDecimal("shuiLv")));//本币税额
            gweeT.setGWEE017("");//销售人员
            gweeT.setGWEE018(shiroUser.getORG_ID());//归属组织
            gweeT.setGWEE022(gwcqT.getGWCQDOCNO());//购物单号
            gweeT.setGWEE101(String.valueOf(gwcqT.getGWCQSEQ()));//项次
            gweeT.setGWEE021("1");
            gweeT.setGWEE023("Y");
            gweeT.setGWEEPLANT(gwcqT.getGWCQPLANT());
            gweeT.setGWEELEGAL(gwcqT.getGWCQLEGAL());
            gweeT.setGWEEMLEGAL(gwcqT.getGWCQMLEGAL());
            gweeT.setGWEEACTI("N");
            gweeT.setGWEEUSER(shiroUser.getID());
            gweeT.setGWEEGRUP(shiroUser.getORG_ID());
            gweeT.setGWEEDATE(LocalDate.now());
            gweeT.setGWEETIME(DateUtils.formatDate(date, "HH:mm:ss"));
            gweeT.setGWEEORIU(shiroUser.getID());
            gweeT.setGWEEORIG(shiroUser.getORG_ID());
            gweeT.insert();
            queryDto.clear();
            queryDto.put("ENT", gwcqT.getGWCQENT());
            queryDto.put("GWCQDOCNO", gwcqT.getGWCQDOCNO());
            queryDto.put("GWCQSEQ", gwcqT.getGWCQSEQ());
            this.baseMapper.setTransformInquiryYes(queryDto);
        }
    }

    /**
     * 客户权限
     *
     * @param dto
     * @return
     */
    private List<Dto> getCustomersAuthority(Dto dto) {
        List<Dto> dtoList = null;
        ShiroUser shiroUser = ShiroKit.getUser();

        Dto queryDto = Dtos.newDto();
        queryDto.put("ENT", shiroUser.getDBC());
        queryDto.put("ID", shiroUser.getID());
        Dto gwb001Dto = this.baseMapper.getGwb001(queryDto);

        if (gwb001Dto.getInteger("QYCOUNT") + gwb001Dto.getInteger("HYCOUNT") > 0) {
            queryDto.put("QYCOUNT", gwb001Dto.getInteger("QYCOUNT"));
            queryDto.put("HYCOUNT", gwb001Dto.getInteger("HYCOUNT"));
            if (dto == null || "".equals(dto.getString("GWAK006"))) {
                queryDto.put("GWAK006", "1");
            } else {
                queryDto.put("GWAK006", dto.getString("GWAK006"));
            }
            dtoList = this.baseMapper.getGwb002(queryDto);
            if (dtoList.size() == 0) {
                Dto defaultDto = Dtos.newDto();
                defaultDto.put("ID", "GWAK001");
                dtoList.add(defaultDto);
            }
        }
        return dtoList;
    }

    /**
     * 部门权限
     *
     * @param dto
     * @return
     */
    private List<Dto> getOrgsAuthority(Dto dto) {
        List<Dto> dtoList = null;
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto queryDto = Dtos.newDto();
        queryDto.put("ID", shiroUser.getID());
        dtoList = this.baseMapper.getSys003(queryDto);
        if (dtoList.size() == 0) {
            Dto defaultDto = Dtos.newDto();
            defaultDto.put("ID", shiroUser.getORG_ID());
            dtoList.add(defaultDto);
        }
        return dtoList;
    }

    /**
     * 获取折扣率、汇率类型、汇率
     *
     * @param customerId
     * @return
     */
    public Dto getCustomerData(String customerId) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        String huiLvLeiXing = this.getHuiLvLeiXing(shiroUser.getDBC(), customerId);
        Dto biZhongShuiZhongShuiLvZheKou = this.getBiZhongShuiZhongShuiLvZheKou(shiroUser.getDBC(), customerId);
        BigDecimal huiLv = this.getHuiLv(shiroUser.getDBC(), customerId, biZhongShuiZhongShuiLvZheKou.getString("biZhong"));
        outDto.put("huiLvLeiXing", huiLvLeiXing);
        outDto.put("huiLv", huiLv);
        outDto.put("zheKou", biZhongShuiZhongShuiLvZheKou.getBigDecimal("zheKou"));
        outDto.put("bizhong", biZhongShuiZhongShuiLvZheKou.getString("bizhong"));
        outDto.put("shuiZhong", biZhongShuiZhongShuiLvZheKou.getString("shuiZhong"));
        outDto.put("shuiLv", biZhongShuiZhongShuiLvZheKou.getBigDecimal("shuiLv"));
        return outDto;
    }

    /**
     * 获取汇率类型
     *
     * @return
     */
    private String getHuiLvLeiXing(String ENT, String customerId) {
        Dto queryDto = Dtos.newDto();
        queryDto.put("ENT", ENT);
        queryDto.put("CUSTOMERID", customerId);
        String huiLvLeiXing = this.baseMapper.getGwak057(queryDto);
        if (huiLvLeiXing == null) {
            huiLvLeiXing = this.baseMapper.getSysParams(queryDto);
        }
        return huiLvLeiXing;
    }

    /**
     * 获取汇率
     *
     * @return
     */
    private BigDecimal getHuiLv(String ENT, String type, String biZhong) {
        Dto queryDto = Dtos.newDto();
        if (YUEHUILV.equals(type)) {
            queryDto.put("ENT", ENT);
            queryDto.put("GWEK001", biZhong);
            queryDto.put("GWEK002", DateUtils.getDate("yyyyMM"));
            BigDecimal huiLv = this.baseMapper.getGwek005(queryDto);
            if (huiLv == null) {
                huiLv = new BigDecimal("1");
            }
            return huiLv;
        } else {
            queryDto.put("ENT", ENT);
            queryDto.put("GWEL001", biZhong);
            queryDto.put("GWEL002", DateUtils.getDate("yyyyMMdd"));
            BigDecimal huiLv = this.baseMapper.getGwel005(queryDto);
            if (huiLv == null) {
                huiLv = new BigDecimal("1");
            }
            return huiLv;
        }
    }

    /**
     * 获取币种，税种，税率,折扣
     *
     * @return
     */
    private Dto getBiZhongShuiZhongShuiLvZheKou(String ENT, String customerId) {
        Dto outDto = Dtos.newDto();
        Dto queryDto = Dtos.newDto();
        queryDto.put("ENT", ENT);
        queryDto.put("CUSTOMERID", customerId);
        Dto biZhongShuiZhongShuiLvZheKou = this.baseMapper.getBiZhongShuiZhongShuiLvZheKou(queryDto);
        if (biZhongShuiZhongShuiLvZheKou != null) {
            outDto.put("biZhong", biZhongShuiZhongShuiLvZheKou.getString("biZhong"));
            outDto.put("shuiZhong", biZhongShuiZhongShuiLvZheKou.getString("shuiZhong"));
            outDto.put("shuiLv", biZhongShuiZhongShuiLvZheKou.getBigDecimal("shuiLv"));
            if ("".equals(biZhongShuiZhongShuiLvZheKou.getString("zheKou"))) {
                //BigDecimal zheKou = this.baseMapper.getZheKouByCustomerType(queryDto);
                //if (zheKou == null) {
                //    outDto.put("zheKou", new BigDecimal("1"));
                //}
                //outDto.put("zheKou", zheKou);
                outDto.put("zheKou", new BigDecimal("1"));
            } else {
                outDto.put("zheKou", biZhongShuiZhongShuiLvZheKou.getBigDecimal("zheKou"));
            }
        } else {
            outDto.put("biZhong", "CNY");
            outDto.put("shuiZhong", "");
            outDto.put("shuiLv", 0);
            outDto.put("zheKou", new BigDecimal("1"));
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGiftInfoData(Dto dto) {
        //获取赠品信息表gwcqz的数据
        List<Dto> listData = this.baseMapper.getGwcqzData(dto);
        //循环赠品信息获取订单总数量/金额、起订数量/金额、最大赠品数量/金额
        for (int i = 0; i < listData.size(); i++) {
            Dto queryDto = dto;
            Dto resultDto = Dtos.newDto();
            queryDto.put("GWCQ109", listData.get(i).getString("GWCQZ005"));
            if (listData.get(i).getString("GWDC007").equals("1")) {//渠道
                Dto gwak = this.baseMapper.getGwak(dto);
                queryDto.put("GWDC016", gwak.getString("GWAK010"));
            } else {//渠道商
                queryDto.put("GWDC016", dto.getString("GWCQ010"));
            }
            if (listData.get(i).getString("GWDC004").equals("1")) {//组合
                resultDto = this.baseMapper.getData1(queryDto);
            } else {//非组合
                queryDto.put("GWCQ001", listData.get(i).getString("GWCQZ006"));
                resultDto = this.baseMapper.getData2(queryDto);
            }
            listData.get(i).put("GWCQ003_SUM", resultDto.getString("GWCQ003_SUM"));
            listData.get(i).put("GWCQ005_SUM", resultDto.getString("GWCQ005_SUM"));
            listData.get(i).put("GWDC020", resultDto.getString("GWDC020"));
            listData.get(i).put("GWDC021", resultDto.getString("GWDC021"));
            listData.get(i).put("GWDC025", resultDto.getString("GWDC025"));
            listData.get(i).put("GWDC026", resultDto.getString("GWDC026"));
        }
        return listData;
    }

    /**
     * 根据当前单号获取赠品可开窗的活动信息
     */
    @Transactional(readOnly = true)
    public List<Dto> getGwdcOpenData(Page<Object> page, Dto dto) {
        //获取该购物单下符合满减满赠的活动数据
        List<Dto> listGwcq109 = this.baseMapper.getGwcq109(dto);
        List<Dto> listData = new ArrayList<>();
        for (int k = 0; k < listGwcq109.size(); k++) {
            if (listGwcq109.get(k).getString("GWCQ111") == "1") {
                listData = this.baseMapper.getGwcqData(page, dto);
            } else {
                listData = this.baseMapper.getGwcqData_(page, dto);
            }
        }
        //循环活动信息获取商品信息、订单总数量/金额、起订数量/金额、最大赠品数量/金额
        for (int i = 0; i < listData.size(); i++) {
            Dto queryDto = dto;
            Dto resultDto = Dtos.newDto();
            queryDto.put("GWCQ109", listData.get(i).getString("GWCQ109"));
            if (listData.get(i).getString("GWDC007").equals("1")) {//渠道
                Dto gwak = this.baseMapper.getGwak(dto);
                queryDto.put("GWDC016", gwak.getString("GWAK010"));
            } else {//渠道商
                queryDto.put("GWDC016", dto.getString("GWCQ010"));
            }
            if (listData.get(i).getString("GWDC004").equals("1")) {//组合
                resultDto = this.baseMapper.getData1(queryDto);
            } else {//非组合
                queryDto.put("GWCQ001", listData.get(i).getString("GWCQ001"));
                resultDto = this.baseMapper.getData2(queryDto);
            }
            listData.get(i).put("GWCQ001", resultDto.getString("GWCQ001"));
            listData.get(i).put("GWAML003", resultDto.getString("GWAML003"));
            listData.get(i).put("GWAML004", resultDto.getString("GWAML004"));
            listData.get(i).put("GWCQ003_SUM", resultDto.getString("GWCQ003_SUM"));
            listData.get(i).put("GWCQ005_SUM", resultDto.getString("GWCQ005_SUM"));
            listData.get(i).put("GWDC020", resultDto.getString("GWDC020"));
            listData.get(i).put("GWDC021", resultDto.getString("GWDC021"));
            listData.get(i).put("GWDC025", resultDto.getString("GWDC025"));
            listData.get(i).put("GWDC026", resultDto.getString("GWDC026"));
            listData.get(i).put("GWDC005", resultDto.getString("GWDC005"));
        }
        return listData;
    }

    /**
     * 获取赠品信息开窗
     */
    @Transactional(readOnly = true)
    public List<Dto> getGwdczOpenData(Page<Object> page, Dto dto) {
        //获取活动数据
        Dto gwdcData = this.baseMapper.getGwdc004(dto);
        Dto queryDto = dto;
        //判断对象类型
        List<Dto> listData = new ArrayList<>();
        if (gwdcData.getString("GWDC007").equals("1")) {//渠道
            Dto gwak = this.baseMapper.getGwak(dto);
            queryDto.put("GWDC016", gwak.getString("GWAK010"));
        } else {
            queryDto.put("GWDC016", dto.getString("GWCQ010"));
        }
        if (gwdcData.getString("GWDC004").equals("1")) {//组合
            /*if(gwdcData.getString("GWDC022").equals("Y")){
                if(!gwdcData.getString("GWDC005").equals("3")){
                    listData = this.baseMapper.getGwdcz1_GWDCT003(page,queryDto);
                }else {
                    listData = this.baseMapper.getGwdcz1_GWDCT004(page,queryDto);
                }

            }else {
                listData = this.baseMapper.getGwdcz1(page,queryDto);
            }*/
            listData = this.baseMapper.getGwdcz1(page, queryDto);
        } else {//非组合
            if (gwdcData.getString("GWDC022").equals("Y")) {

                if (!gwdcData.getString("GWDC005").equals("3")) {
//                    Dto gwdc = this.baseMapper.getGWDC002(queryDto);
                    Dto gwdc = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGWDC002",queryDto);
                    if (!gwdc.getBigDecimal("GWDC002").equals(0) && gwdc.size() > 0) {
                        queryDto.put("GWDC002", gwdc.getBigDecimal("GWDC002"));
                    }
                    listData = this.baseMapper.getGwdcz2_GWDC020(page, queryDto);
                } else {
//                    Dto gwdc = this.baseMapper.getGWDC002_(queryDto);
                    Dto gwdc = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGWDC002_",queryDto);
                    if (!gwdc.getBigDecimal("GWDC002").equals(0) && gwdc.size() > 0) {
                        queryDto.put("GWDC002", gwdc.getBigDecimal("GWDC002"));
                    }
                    listData = this.baseMapper.getGwdcz2_GWDC021(page, queryDto);
                }

            } else {
                listData = this.baseMapper.getGwdcz2(page, queryDto);
            }
        }
        return listData;
    }

    @Transactional
    public void addGwcqzData(List<GwcqzT> gwcqzTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String GWCQZDOCNO = gwcqzTList.get(0).getGWCQZDOCNO();
        GwcqzT gwcqzT = new GwcqzT();
        UpdateWrapper<GwcqzT> gwcqzTUpdateWrapper = new UpdateWrapper<>();
        gwcqzTUpdateWrapper.eq("GWCQZENT", shiroUser.getDBC());
        gwcqzTUpdateWrapper.eq("GWCQZDOCNO", GWCQZDOCNO);
        gwcqzT.delete(gwcqzTUpdateWrapper);
        //增加赠品明细
        for (GwcqzT gwcqzT1 : gwcqzTList) {
            gwcqzT1.setGWCQZENT(shiroUser.getDBC());
            gwcqzT1.insert();
        }
    }

    @Transactional
    public Dto getGwdc020_021(Dto dto) {
        Dto gwdc = this.baseMapper.getGwdc004_007(dto);
        return gwdc;
    }

    @Transactional
    public void delGwcqzByGwcqz007(Dto dto) {
        this.baseMapper.delGwcqzByGwcqz007(dto);
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwcqData(Dto dto) {
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
//        List<Dto> result = this.baseMapper.getGwdc001(dto);
        List<Dto> result = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwdc001",dto);
        for (int i = 0; i < result.size(); i++) {
            Dto inDto = Dtos.newDto();
            Dto outDto = Dtos.newDto();
            List<Dto> list = new ArrayList<>();
            inDto.put("GWCQ010", result.get(i).getString("GWCQ010"));
            if (!(result.get(i).getString("GWCQ109").equals("") && result.get(i).getString("GWCQ109").isEmpty())) {
                inDto.put("GWCQ109", result.get(i).getString("GWCQ109"));
                //购物车活动有阶梯先根据活动抓取购物车信息
//                list = this.baseMapper.getGwcqList1(inDto);
                list = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList1",inDto);
                BigDecimal count = new BigDecimal(0);
                for (int j = 0; j < list.size(); j++) {
                    count = count.add(list.get(j).getBigDecimal("GWCQ005"));
                }
                /*List<Dto> gwdcz = this.baseMapper.getGwdcz(inDto);
                result.get(i).put("result", gwdcz);
                result.get(i).put("length", gwdcz.size());*/
                result.get(i).put("count", count);
                if (result.get(i).getString("GWDC007").equals("1")) {//渠道
                    dto.put("GWCQ010", result.get(i).getString("GWCQ010"));
                    Dto gwak = this.baseMapper.getGwak(dto);
                    dto.put("GWDC016", gwak.getString("GWAK010"));
                    inDto.put("GWDC016", gwak.getString("GWAK010"));
                } else {
                    dto.put("GWDC016", result.get(i).getString("GWCQ010"));
                    inDto.put("GWDC016", result.get(i).getString("GWCQ010"));
                }
                //根据活动获取赠品
                List<Dto> gwdcz = new ArrayList<>();
                if(result.get(i).getString("GWDC004").equals("1")){ //组合活动
//                    gwdcz = this.baseMapper.getGwdcz(inDto);
                    gwdcz = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwdcz",inDto);
                }else { //非组合活动
                   for (int j = 0; j < list.size(); j++) {
                    inDto.put("GWCQ001", list.get(j).getString("GWCQ001"));
                    List<Dto> gwdczList = new ArrayList<>();
//                    gwdcz = this.baseMapper.getGwdcz_jt2(inDto);
                    gwdczList = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwdcz_jt2",inDto);
                    gwdcz.addAll(gwdczList);
                }
                }
                result.get(i).put("result", gwdcz);
                result.get(i).put("length", gwdcz.size());
            } else {
//                list = this.baseMapper.getGwcqList2(inDto);
                list = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList2",inDto);
                BigDecimal count = new BigDecimal(0);
                for (int j = 0; j < list.size(); j++) {
                    count = count.add(list.get(j).getBigDecimal("GWCQ005"));
                }
                result.get(i).put("length", 0);
                result.get(i).put("result", "");
                result.get(i).put("count", count);
            }
            result.get(i).put("checked", false);
            result.get(i).put("date", DateUtils.getDate("yyyy-MM-dd"));
            result.get(i).put("data", list);
        }
        return result;
    }

    @Transactional
    public void deleteGwcqBatch(GwcqT gwcqT) {
        if(gwcqT.getGWCQ109()==null){
            Dto dto = Dtos.newDto();
            dto.put("GWCQ010", gwcqT.getGWCQ010());
            this.baseMapper.deleteGwcq(dto);
        }else if (gwcqT.getGWCQ109().equals("")) {
            Dto dto = Dtos.newDto();
            dto.put("GWCQ010", gwcqT.getGWCQ010());
            this.baseMapper.deleteGwcq(dto);
        } else {
            UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
            gwcqTUpdateWrapper.eq("GWCQ010", gwcqT.getGWCQ010());
            gwcqTUpdateWrapper.eq("GWCQ109", gwcqT.getGWCQ109());
            gwcqTUpdateWrapper.eq("GWCQACTI", "Y");
            gwcqT.delete(gwcqTUpdateWrapper);
        }
    }

    @Transactional
    public void addData(GwcqT gwcqTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto customerData = this.baseMapper.getBizhong(gwcqTList.getGWCQ010());
        String gwcqdocno = "";
        //增加新的明细
        Date date = new Date();
        Dto dto = Dtos.newDto();
        dto.put("GWCQ010", gwcqTList.getGWCQ010());
        dto.put("GWCQ109", gwcqTList.getGWCQ109());
        List<Dto> list = new ArrayList<>();
        if (gwcqTList.getGWCQ109() == null) {
//            list = this.baseMapper.getGwcqList2(dto);
            list = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList2",dto);
        } else {
//            list = this.baseMapper.getGwcqList1(dto);
            list = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList1",dto);
        }
        if (list.size() > 0) {
            dto.put("GWCQ001", gwcqTList.getGWCQ001());
            List<Dto> list1 = new ArrayList<>();
            if (gwcqTList.getGWCQ109() == null) {
//                list1 = this.baseMapper.getGwcqList2(dto);
                list1 = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList2",dto);
            } else {
//                list1 = this.baseMapper.getGwcqList1(dto);
                list1 = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList1",dto);
            }
            if (list1.size() > 0) {
                gwcqdocno = list1.get(0).getString("GWCQDOCNO");
                UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
                gwcqTList.setGWCQDOCNO(gwcqdocno);
                gwcqTList.setGWCQSEQ(list1.get(0).getBigDecimal("GWCQSEQ"));
                gwcqTList.setGWCQ003(list1.get(0).getBigDecimal("GWCQ003").add(gwcqTList.getGWCQ003()));
                gwcqTList.setGWCQ005(list1.get(0).getBigDecimal("GWCQ005").add(gwcqTList.getGWCQ005()));
                gwcqTList.setGWCQ104(gwcqTList.getGWCQ005());
                gwcqTUpdateWrapper.eq("GWCQDOCNO", gwcqdocno);
                gwcqTUpdateWrapper.eq("GWCQSEQ", list1.get(0).getString("GWCQSEQ"));
                gwcqTUpdateWrapper.eq("GWCQACTI", "Y");
                gwcqTList.update(gwcqTUpdateWrapper);
            } else {
                LocalDate gwcqdocdate = LocalDate.now();
                gwcqTList.setGWCQDOCNO(list.get(0).getString("GWCQDOCNO"));
                gwcqTList.setGWCQDOCDATE(gwcqdocdate);
                initData(gwcqTList, list.size() + 1, shiroUser);
                gwcqTList.setGWCQ006(customerData.getString("GWAK008"));
                gwcqTList.setGWCQPLANT(customerData.getString("GWAKPLANT"));
                gwcqTList.setGWCQLEGAL(customerData.getString("GWAKMLEGAL"));
                gwcqTList.setGWCQ103(gwcqTList.getGWCQ004().divide(gwcqTList.getGWCQ113(), 6, ROUND_HALF_DOWN));
                gwcqTList.setGWCQ104(gwcqTList.getGWCQ005());
                gwcqTList.setGWCQACTI("Y");
                gwcqTList.setGWCQ101(DateUtils.getDate());
                gwcqTList.insert();
            }
        } else {
            gwcqdocno = "GWDD" + DateUtils.formatDate(date, "yyyyMMddHHmmss");
            LocalDate gwcqdocdate = LocalDate.now();
            gwcqTList.setGWCQDOCNO(gwcqdocno);
            gwcqTList.setGWCQDOCDATE(gwcqdocdate);
            initData(gwcqTList, 1, shiroUser);
            gwcqTList.setGWCQ006(customerData.getString("GWAK008"));
            gwcqTList.setGWCQPLANT(customerData.getString("GWAKPLANT"));
            gwcqTList.setGWCQLEGAL(customerData.getString("GWAKMLEGAL"));
            gwcqTList.setGWCQ103(gwcqTList.getGWCQ004().divide(gwcqTList.getGWCQ113(), 6, ROUND_HALF_DOWN));
            gwcqTList.setGWCQ104(gwcqTList.getGWCQ005());
            gwcqTList.setGWCQACTI("Y");
            gwcqTList.setGWCQ101(DateUtils.getDate());
            gwcqTList.insert();
        }


    }
}
